//
// Source code recreated from a .class file by IntelliJ IDEA
// (powered by Fernflower decompiler)
//

package org.quickserver.net.qsadmin.gui;

import java.awt.Container;
import java.awt.Dimension;
import java.awt.GridBagConstraints;
import java.awt.event.ActionListener;
import java.awt.event.ItemEvent;
import java.awt.event.ItemListener;
import java.util.StringTokenizer;
import javax.swing.Box;
import javax.swing.JButton;
import javax.swing.JComboBox;
import javax.swing.JLabel;
import javax.swing.JTextField;
import org.quickserver.util.MyString;

public class Propertie {
    private String name;
    private String target = "server";
    private String command;
    private boolean get = false;
    private boolean set = false;
    private String type = "edit";
    private String select;
    private String desc;
    private String targetNeeded = "yes";
    private String version = "1.3";
    private JLabel namelabel;
    private JTextField editField;
    private JComboBox selectList;
    private JButton saveButton;

    public Propertie() {
    }

    public String getGetCommand() {
        return this.targetNeeded.equals("yes") ? "get " + this.target + " " + this.command : "get " + this.command;
    }

    public String getSetCommand(String value) {
        return this.targetNeeded.equals("yes") ? "set " + this.target + " " + this.command + " " + value : "set " + this.command + " " + value;
    }

    public String getName() {
        return this.name;
    }

    public void setName(String name) {
        if (name != null && !name.equals("")) {
            this.name = name;
        }

    }

    public String getTarget() {
        return this.target;
    }

    public void setTarget(String target) {
        if (target != null && !target.equals("")) {
            this.target = target;
        }

    }

    public String getCommand() {
        return this.command;
    }

    public void setCommand(String command) {
        if (command != null && !command.equals("")) {
            this.command = command;
        }

    }

    public void setGet(String getValue) {
        if (getValue != null && getValue.toLowerCase().equals("yes")) {
            this.get = true;
        } else {
            this.get = false;
        }

    }

    public boolean isGet() {
        return this.get;
    }

    public void setSet(String setValue) {
        if (setValue != null && setValue.toLowerCase().equals("yes")) {
            this.set = true;
        } else {
            this.set = false;
        }

    }

    public boolean isSet() {
        return this.set;
    }

    public String getType() {
        return this.type;
    }

    public void setType(String type) {
        if (type != null && !type.equals("")) {
            this.type = type;
        }

    }

    public String getDesc() {
        return this.desc;
    }

    public void setDesc(String desc) {
        if (desc != null && !desc.equals("")) {
            this.desc = desc;
        }

    }

    public String getSelect() {
        return this.select;
    }

    public void setSelect(String select) {
        if (select != null && !select.equals("")) {
            this.select = select;
        }

    }

    public String getTargetNeeded() {
        return this.targetNeeded;
    }

    public void setTargetNeeded(String targetNeeded) {
        this.targetNeeded = targetNeeded.toLowerCase();
    }

    public String getVersion() {
        return this.version;
    }

    public float getVersionNo() {
        String ver = this.version;
        float version = 0.0F;
        int i = ver.indexOf(" ");
        if (i == -1) {
            i = ver.length();
        }

        ver = ver.substring(0, i);
        i = ver.indexOf(".");
        if (i != -1) {
            int j = ver.indexOf(".", i);
            if (j != -1) {
                ver = ver.substring(0, i) + "." + MyString.replaceAll(ver.substring(i + 1), ".", "");
            }
        }

        try {
            version = Float.parseFloat(ver);
        } catch (NumberFormatException var5) {
            ;
        }

        return version;
    }

    public void setVersion(String version) {
        if (version != null && !version.equals("")) {
            this.version = version.toLowerCase();
        }

    }

    public String toXML() {
        StringBuilder sb = new StringBuilder();
        sb.append("<propertie>\n");
        sb.append("\t<name>").append(this.name).append("</name>\n");
        sb.append("\t<command>").append(this.command).append("</command>\n");
        if (this.get) {
            sb.append("\t<get>yes</get>\n");
        } else {
            sb.append("\t<get>no</get>\n");
        }

        if (this.set) {
            sb.append("\t<set>yes</set>\n");
        } else {
            sb.append("\t<set>no</set>\n");
        }

        sb.append("\t<type>").append(this.type).append("</type>\n");
        if (this.select != null) {
            sb.append("\t<select>").append(this.select).append("</select>\n");
        }

        if (this.desc != null) {
            sb.append("\t<desc>").append(this.desc).append("</desc>\n");
        }

        sb.append("\t<version>").append(this.version).append("</version>\n");
        if (this.targetNeeded != null && this.targetNeeded.equals("yes")) {
            sb.append("\t<target-needed>yes</target-needed>\n");
        } else {
            sb.append("\t<target-needed>no</target-needed>\n");
        }

        sb.append("</propertie>\n");
        return sb.toString();
    }

    public void load(PropertiePanel pp, QSAdminMain qsadminMain) {
        this.setTarget(pp.getTarget());
        String temp = null;
        if (!this.isGet()) {
            temp = "+OK  ";
        } else {
            try {
                temp = qsadminMain.sendCommunicationSilent(this.getGetCommand(), false, false);
            } catch (Exception var5) {
                temp = "Could not get parameter : " + var5.getMessage();
            }
        }

        if (temp != null) {
            boolean got = false;
            if (temp.startsWith("+OK")) {
                got = true;
            }

            temp = temp.substring(temp.indexOf(" ") + 1);
            if (this.getType().equals("edit")) {
                this.editField.setText(temp);
                if (got) {
                    this.editField.setEnabled(true);
                    this.editField.setEditable(this.isSet());
                }
            } else if (this.getType().equals("select")) {
                this.selectList.setSelectedItem(temp);
                if (got) {
                    this.selectList.setEnabled(true);
                }
            }

        }
    }

    public void addToPanel(Container cp, GridBagConstraints gbc, PropertiePanel pp, QSAdminMain qsadminMain) {
        gbc.weighty = 0.0D;
        gbc.weightx = 0.0D;
        ++gbc.gridy;
        gbc.gridheight = 1;
        gbc.gridwidth = 1;
        gbc.anchor = 17;
        gbc.fill = 0;
        String temp = this.getType().toLowerCase();
        if (temp == null) {
            temp = "edit";
        }

        gbc.gridx = 0;
        gbc.weightx = 0.0D;
        gbc.anchor = 17;
        gbc.fill = 0;
        cp.add(Box.createRigidArea(new Dimension(10, 10)), gbc);
        ++gbc.gridx;
        gbc.anchor = 17;
        gbc.fill = 0;
        this.namelabel = new JLabel(this.getName());
        this.namelabel.setToolTipText(this.getDesc());
        cp.add(this.namelabel, gbc);
        ++gbc.gridx;
        gbc.weightx = 0.0D;
        gbc.anchor = 17;
        gbc.fill = 0;
        cp.add(Box.createRigidArea(new Dimension(10, 10)), gbc);
        ++gbc.gridx;
        gbc.weightx = 1.0D;
        gbc.fill = 2;
        if (temp.equals("edit")) {
            this.editField = new JTextField();
            this.editField.setEnabled(false);
            this.editField.setToolTipText(this.getDesc());
            cp.add(this.editField, gbc);
        } else if (temp.equals("select")) {
            temp = this.getSelect();
            StringTokenizer st = new StringTokenizer(temp, "|");
            String[] valStrings = new String[st.countTokens()];

            for(int i = 0; st.hasMoreTokens(); ++i) {
                valStrings[i] = st.nextToken();
            }

            this.selectList = new JComboBox(valStrings);
            this.selectList.setMaximumRowCount(3);
            this.selectList.setEditable(false);
            this.selectList.setEnabled(false);
            cp.add(this.selectList, gbc);
        }

        gbc.weightx = 0.0D;
        gbc.anchor = 17;
        gbc.fill = 0;
        ++gbc.gridx;
        cp.add(Box.createRigidArea(new Dimension(10, 10)), gbc);
        ++gbc.gridx;
        gbc.weightx = 0.5D;
        gbc.fill = 2;
        if (this.isSet()) {
            this.saveButton = new JButton("Save");
            this.saveButton.setEnabled(false);
            this.saveButton.addActionListener(this.getSaveAction(qsadminMain, this));
            cp.add(this.saveButton, gbc);
        } else {
            cp.add(new JLabel(), gbc);
        }

        ++gbc.gridx;
        gbc.weightx = 0.0D;
        gbc.anchor = 17;
        gbc.fill = 0;
        cp.add(Box.createRigidArea(new Dimension(10, 10)), gbc);
        if (temp.equals("edit")) {
            this.editField.getDocument().addDocumentListener(new EditFieldDocumentListener(this.saveButton));
        } else {
            this.selectList.addItemListener(new ItemListener() {
                public void itemStateChanged(ItemEvent e) {
                    Propertie.this.saveButton.setEnabled(true);
                }
            });
        }

    }

    public JTextField getEditField() {
        return this.editField;
    }

    public JComboBox getComboBox() {
        return this.selectList;
    }

    public JButton getSaveButton() {
        return this.saveButton;
    }

    private ActionListener getSaveAction(QSAdminMain qsadminMain, Propertie propertie) {
        return new SaveActionListener(qsadminMain, propertie);
    }
}
